<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.module" placeholder="模块" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.pid" :clearable="true" :filterable="true">
          <el-option
            v-for="item in pidList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="id">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="value"
        header-align="center"
        align="center"
        label="数值">
      </el-table-column>
      <el-table-column
        prop="pid"
        header-align="center"
        align="center"
        label="pid">
      </el-table-column>
      <el-table-column
        prop="pidName"
        header-align="center"
        align="center"
        label="pid名称">
      </el-table-column>
      <el-table-column
        prop="module"
        header-align="center"
        align="center"
        label="模块">
      </el-table-column>
      <el-table-column
        prop="sortNum"
        header-align="center"
        align="center"
        label="排序">
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" :pid-list="pidList" ref="addOrUpdate" @refreshDataList="reloadData"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './sysdict-add-or-update'
  import httpN from '@/utils/http'
  export default {
    data () {
      return {
        dataForm: {
          pid: '',
          module: ''
        },
        pidList:[],
        dataList: [],
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getPidList()
    },
    methods: {
      // 获取数据列表
      async getSysDictList (pid=0) {
        let data = await httpN.postQ('/sys/sysdict/list', {'pid': pid, module: this.dataForm.module})
        return data
      },
      async getPidList(){
        this.pidList = await this.getSysDictList(0)
        this.pidList.unshift({id:0, name:'字典根目录'})
      },
      async getDataList(){
        if(this.dataForm.pid===''){
          this.dataList = await this.getSysDictList(0)
        }else{
          this.dataList = await this.getSysDictList(this.dataForm.pid)
        }

      },
      reloadData(){
        this.getDataList()
        this.getPidList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/sysdict/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                  this.getPidList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
